<template>
	<view class="all">
		<Header title="学达鲸" :isHome="false" :isWhite='true'></Header>

		<swiper class="banner" circular autoplay="true" interval="2000" duration="500">
			<swiper-item v-for="i in bannerInfo" @click="goBannerDetail(i)">
				<image :src="imgUrl+ i.image" mode=""></image>
			</swiper-item>
		</swiper>

		<view class="flex">
			<view class="" v-for="(item,index) in goPageList" :key="index" @click="goPage(item.url)">
				<view class="">
					<image :src="item.icon" mode=""></image>
				</view>
				<view class="name">
					{{item.name}}
				</view>
			</view>
		</view>
		<view class="discipline">
			<view class="discipline-detail" :class="{ 'discipline-detail-active': activeIndex === item.name }"
				v-for="(item,index) in discipline" :key="index" @click="changeTab(item.name)">
				{{item.name}}
			</view>
		</view>
		<view class="" v-if="itm.totalTitle&&itm.num!=0" v-for="(itm,idx) in goPageList" :key="idx">
			<view class="flex">
				<view class="flex1">
					<view class="totalTitle">
						{{itm.totalTitle}}
					</view>
					<view class="">
						<image :src="itm.icons" mode="" class="icon"></image>
					</view>
				</view>
				<view class="flex1" @click="gotoPage(itm.url)">
					<view class="more">
						更多
					</view>
					<view class="">
						<u-icon name="arrow-right" color="#222" size="12"></u-icon>
					</view>
				</view>
			</view>
			<view class="flex subject" v-if="itm.totalTitle=='天天练习'" v-for="(item,index) in typeList.tb">
				<image src="/static/three.png" mode="" class="three"></image>
				<view class="leftImg">
					<image :src="imgUrl+ item.cover_image" mode=""></image>
				</view>
				<view class="">
					<view class="title">
						{{item.title}}
					</view>
					<view class="content">
						{{item.desc}}
					</view>
					<view class="numAndBtn">
						<view class="flex1" style="margin: 15rpx 0;">
							<view class="num">
								题目数量：<span>{{item.quantity}}</span>
							</view>
						</view>
						<view class="startAnswer" @click="goPage('/subpages/banner/examination?id='+item.id)">
							开始答题
						</view>
					</view>
				</view>
			</view>
			<view class="flex subject" v-if="itm.totalTitle=='基础闯关'" v-for="(item,index) in typeList.jc">
				<image src="/static/three.png" mode="" class="three"></image>
				<view class="leftImg">
					<image :src="imgUrl+ item.cover_image" mode=""></image>
				</view>
				<view class="">
					<view class="title">
						{{item.title}}
					</view>
					<view class="content">
						{{item.desc}}
					</view>
					<view class="numAndBtn">
						<view class="flex1" style="margin: 15rpx 0;">
							<view class="num">
								题目数量：<span>{{item.quantity}}</span>
							</view>
						</view>
						<view class="startAnswer" @click="goPage('/subpages/banner/basicexamination?id='+item.id)">
							开始答题
						</view>
					</view>
				</view>
			</view>
			<view class="flex subject" v-if="itm.totalTitle=='错题本'" v-for="(item,index) in typeList.ct"
				style="height: 100rpx;display: flex;align-items: center;">
				<image src="/static/three.png" mode="" class="three"></image>
				<!-- <view class="leftImg">
					<image :src="imgUrl+ item.cover_image" mode=""></image>
				</view> -->
				<view class="" style="width: 100%;display: flex;align-items: center;justify-content: space-between; ">
					<view class="title">
						{{item.topic}}
					</view>
					<!-- <view class="content">
						{{item.desc}}
					</view> -->
					<view class="numAndBtn">
						<view class="wrong">
							<view class="sameType"
								@click="goPage('/subpages/banner/sameSubject?id='+item.questions_id)">
								同类访题
							</view>
							<view class="startAnswer" @click="goPage('/subpages/banner/wrongPractice?id='+item.id)">
								练习
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="flex subject" v-if="itm.totalTitle=='单元测评'" v-for="(item,index) in typeList.dy">
				<image src="/static/three.png" mode="" class="three"></image>
				<view class="leftImg">
					<image :src="imgUrl+ item.cover_image" mode=""></image>
				</view>
				<view class="">
					<view class="title">
						{{item.title}}
					</view>
					<view class="numAndBtn">
						<view class="flex1" style="margin: 15rpx 0;">
							<view class="num mark">
								试卷总分：<span>{{item.total_score}}</span>
							</view>
							<view class="num">
								题目数量：<span>{{item.quantity}}</span>
							</view>
						</view>
					</view>
					<view class="test">
						<view class="startAnswer" @click="goPage('/subpages/banner/uniexamination?id='+item.id)">
							开始考试
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		get,
		img_url,
		post
	} from '@/utils/request.js'
	export default {
		data() {
			return {
				imgUrl: img_url,
				userInfo: '',
				goPageList: [{
						id: 1,
						name: '同步训练',
						totalTitle: '天天练习',
						icon: '/static/a1.png',
						icons: '/static/title1.png',
						url: '/subpages/banner/synchronization'
					},
					{
						id: 2,
						name: '基础训练',
						totalTitle: '基础闯关',
						icon: '/static/a2.png',
						icons: '/static/title2.png',
						url: '/subpages/banner/basictraining'
					},
					{
						id: 3,
						name: '错题训练',
						totalTitle: '错题本',
						icon: '/static/a3.png',
						icons: '/static/title3.png',
						url: '/subpages/banner/wrongtraining',
						num: 0
					},
					{
						id: 4,
						name: '单元测试',
						totalTitle: '单元测评',
						icon: '/static/a4.png',
						icons: '/static/title4.png',
						url: '/subpages/banner/unittest'
					},
					{
						id: 5,
						name: '超强大脑',
						icon: '/static/a5.png',
						url: '/subpages/banner/superbrain'
					}
				],
				discipline: [{
						name: '语文',
					},
					{
						name: '数学',
					},
					{
						name: '英语',
					}
				],
				activeIndex: '语文',
				bannerInfo: {},
				typeList: [{
					id: 1,
					totalTitle: '天天练习',
					icon: '/static/title1.png',
					bookImg: '/static/book.png',
					title: '小节题目标题题目标题题目标题',
					content: '题目简介题目简介题目简介题目简介题目简介题目简介',
					num: '38',
					type: '1', //天天练习
					discipline: '1', //语文
					url: '/subpages/banner/synchronization'
				}],
				cuotiNum: 0
			}
		},
		onLoad(options) {
			if(options&& options.query){
				var query = options.query;
			}
			if (query&&query.scene) { // 1047 表示用户通过扫描小程序码进入小程序
				const scene = decodeURIComponent(options.query.scene);
				console.log('scene:',scene)
				const params = {};
				const paramPairs = scene.split('&');
				paramPairs.forEach(pair => {
					const [key, value] = pair.split('=');
					params[key] = value;
				});
				console.log('paramPairs:',paramPairs)
				const inviteMobile = params.invite_mobile;
				console.log('邀请手机号:',inviteMobile);
				uni.setStorageSync('invite_mobile', inviteMobile)
			}
			this.getBanner()
		},
		onShow(options) {
			if(options&& options.query){
				var query = options.query;
			}
			if (query&&query.scene) { // 1047 表示用户通过扫描小程序码进入小程序
				const scene = decodeURIComponent(options.query.scene);
				console.log('scene:',scene)
				const params = {};
				const paramPairs = scene.split('&');
				paramPairs.forEach(pair => {
					const [key, value] = pair.split('=');
					params[key] = value;
				});
				console.log('paramPairs:',paramPairs)
				const inviteMobile = params.invite_mobile;
				console.log('邀请手机号:',inviteMobile);
				uni.setStorageSync('invite_mobile', inviteMobile)
			}
			this.getUserInfo()
			this.getIndex()
		},
		methods: {
			/**
			 * URL转对象
			 * @author 深圳前海万联科技有限公司 <www.wanlshop.com>
			 */
			getParam(url) {
				var pms = {},
					keyValue = url.indexOf('?') != -1 ? url.split('?')[1].split('&') : url.split('&');
				for (var i = 0; i < keyValue.length; i++) {
					var item = keyValue[i].split('=');
					var key = item[0];
					var value = item[1];
					pms[key] = value;
				}
				return pms;
			},
			// 获取首页信息
			getIndex() {
				get('/index/indexs?searchText=' + this.activeIndex)
					.then(res => {
						if (res.code == 1) {
							this.typeList = res.data
							this.cuotiNum = this.typeList.ct.length
							for (let i = 0; i < this.goPageList.length; i++) {
								if (this.goPageList[i].id == 3) {
									this.goPageList[i].num = this.cuotiNum;
									break;
								}
							}
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
					.catch(err => {
						console.error('GET 请求失败:', err);
					});
			},
			getBanner() {
				get('/index/banners').then(res => {
					if (res.code == 1) {
						this.bannerInfo = res.data
					}
				})
			},
			goBannerDetail(i) {
				const token = uni.getStorageSync('token')
				if (!token) {
					uni.showToast({
						title:'请登录后操作！',
						icon:'none'
					})
					setTimeout(function(){
						uni.redirectTo({
							url: '/subpages/login/login'
						})
					},1000)
					return
				}
				if (this.userInfo.is_exper == 1) {
					console.log(i);
					if (i.type == "paper") {
						if (i.module_id == 1) {
							uni.navigateTo({
								url: '/subpages/banner/examination?id=' + i.src
							})
						} else if (i.module_id == 2) {
							uni.navigateTo({
								url: '/subpages/banner/basicexamination?id=' + i.src
							})
						} else if (i.module_id == 4) {
							uni.navigateTo({
								url: '/subpages/banner/uniexamination?id=' + i.src
							})
						}

					} else if (i.type == "article") {
						uni.navigateTo({
							url: '/subpages/banner/detail?id=' + i.src
						})
					} else if (i.type == "customize") {
						uni.navigateTo({
							url: '/subpages/banner/detail1?content=' + i.content + '&title=' + i.title
						})
					}
				} else {
					uni.showToast({
						title: '暂无权限，快去开通会员吧~',
						icon: 'none'
					})
				}
			},
			goPage(url) {
				const token = uni.getStorageSync('token')
				if (!token) {
					uni.showToast({
						title:'请登录后操作！',
						icon:'none'
					})
					setTimeout(function(){
						uni.redirectTo({
							url: '/subpages/login/login'
						})
					},1000)
					return
				}
				if (this.userInfo.is_exper == 1) {
					uni.navigateTo({
						url: url
					})
				} else {
					uni.showToast({
						title: '暂无权限，快去开通会员吧~',
						icon: 'none'
					})
				}
			},
			gotoPage(url) {
				if (this.userInfo.is_exper == 1) {
					uni.navigateTo({
						url: url + '?text=' + this.activeIndex
					})
				} else {
					uni.showToast({
						title: '暂无权限，快去开通会员吧~',
						icon: 'none'
					})
				}
			},
			changeTab(index) {
				console.log(index);
				this.activeIndex = index
				this.getIndex()
			},
			getUserInfo() {
				get('/user/user_info')
					.then(res => {
						if (res.code == 1) {
							this.userInfo = res.data
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
					.catch(err => {
						console.error('GET 请求失败:', err);
					});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.all {
		padding-bottom: 100rpx;
	}

	.banner {
		image {
			width: 686rpx;
			height: 250rpx;
			display: block;
			margin: 20rpx auto;
			border-radius: 20rpx;
		}

	}

	.flex {
		padding: 0 40rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		image {
			width: 100rpx;
			height: 100rpx;
		}

		.name {
			font-size: 24rpx;
			color: #222;
			font-weight: 600;
			font-family: PingFangSC;
		}
	}

	.discipline {
		display: flex;
		justify-content: space-around;
		align-items: center;
		width: 686rpx;
		height: 138rpx;
		background-color: #1A1A1A;
		margin: 20rpx auto;
		border-radius: 24rpx;

		.discipline-detail {
			width: 180rpx;
			height: 74rpx;
			background: #fff;
			border-radius: 37rpx;
			text-align: center;
			line-height: 74rpx;
			font-family: PingFangSC-SNaNrpxibold;
			font-weight: 600;
			font-size: 32rpx;
			color: #1A1A1A;
		}

		.discipline-detail-active {
			width: 180rpx;
			height: 74rpx;
			background: #FFD275;
			border-radius: 37rpx;
			text-align: center;
			line-height: 74rpx;
			font-family: PingFangSC-SNaNrpxibold;
			font-weight: 600;
			font-size: 32rpx;
			color: #1A1A1A;
		}
	}



	.flex1 {
		display: flex;
		align-items: center;

		.totalTitle {
			font-family: PingFangSC-SNaNrpxibold;
			font-weight: 600;
			font-size: 32rpx;
			color: #222222;
			line-height: 44rpx;
			margin: 20rpx 20rpx 20rpx 0;
		}

		.icon {
			width: 52rpx;
			height: 32rpx;
			display: block;
		}
	}

	.more {
		font-family: PingFangSC-Regular;
		font-weight: 400;
		font-size: 24rpx;
		color: #222222;
	}

	.leftImg {
		border-radius: 24rpx;

		image {
			width: 142rpx;
			height: 142rpx;
			display: block;
			margin-right: 20rpx;
			border-radius: 15rpx;
		}
	}

	.subject {
		width: 686rpx;
		height: 206rpx;
		padding: 20rpx;
		box-sizing: border-box;
		background-color: #fff;
		border: 3rpx solid #222222;
		border-radius: 12rpx;
		margin: 20rpx auto;
		position: relative;

		.startAnswer {
			width: 120rpx;
			height: 48rpx;
			background: #FFD275;
			border-radius: 6rpx;
			font-family: PingFangSC-Regular;
			font-weight: 400;
			font-size: 22rpx;
			color: #1A1A1A;
			text-align: center;
			line-height: 48rpx;
		}

		.test {
			width: 468rpx;
			display: flex;
			justify-content: flex-end;
		}

		.three {
			width: 66rpx;
			height: 34rpx;
			position: absolute;
			top: -12rpx;
			right: 54rpx;
		}

		.title {
			font-family: PingFangSC-SNaNrpxibold;
			font-weight: 600;
			font-size: 26rpx;
			color: #222222;
			white-space: nowrap;
			/* 防止文本换行 */
			overflow: hidden;
			/* 隐藏溢出部分 */
			text-overflow: ellipsis;
		}

		.content {
			width: 468rpx;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
			font-family: PingFangSC-Regular;
			font-weight: 400;
			font-size: 24rpx;
			color: #999999;
			margin: 20rpx 0;
		}

		.num {
			padding: 0 10rpx;
			height: 32rpx;
			background: #EEEEEE;
			border: 1rpx solid #DDDDDD;
			border-radius: 4rpx;
			text-align: center;
			line-height: 32rpx;
			font-family: PingFangSC-Regular;
			font-weight: 400;
			font-size: 23rpx;
			color: #555555;

			span {
				font-family: PingFangSC-SNaNrpxibold;
				font-weight: 600;
				font-size: 23rpx;
				color: #222222;
			}
		}

		.mark {
			margin-right: 20rpx;
		}

		.numAndBtn {
			display: flex;
			justify-content: space-between;
			align-items: center;



			.wrong {
				display: flex;
				width: 468rpx;
				justify-content: flex-end;
				align-items: center;
			}

			.sameType {
				width: 120rpx;
				height: 48rpx;
				border: 2rpx solid #1A1A1A;
				border-radius: 6rpx;
				font-family: PingFangSC-Regular;
				font-weight: 400;
				font-size: 22rpx;
				color: #1A1A1A;
				text-align: center;
				line-height: 48rpx;
				margin-right: 20rpx;
			}
		}


	}
</style>